import React from 'react';
import { Modal, message } from 'antd/lib/index';
import axios from './axios';
import QuickTool from './QuickTool';

export default {
  //搜索面板数据映射
  mapping(params) {
    let obj = {};
    params.state.searchFieldList
      .filter(item => {
        return item.type == 'BUTTON';
      })
      .map(item => {
        if (!item['callback']) {
          obj[item.field] = function(searchReturnData) {
            params.page.page = 1;
            params.page = { ...params.page, ...searchReturnData };
            params.getListForPage();
            params.setState({
              loadingTableisHide: false,
            });
          };
        } else if (!item['callback']['mappingfun']) {
          item['callback'] &&
            (obj[item['callback']['name']] = function(searchReturnData) {
              params.page.page = 1;
              params.page = { ...params.page, ...searchReturnData };
              params.getListForPage();
              params.setState({
                loadingTableisHide: false,
              });
            });
        } else {
          item['callback'] && (obj[item['callback']['name']] = item['callback']['mappingfun']);
        }
      });

    obj['search'] = searchReturnData => {
      params.page.page = 1;
      params.page = { ...params.page, ...searchReturnData };
      params.getListForPage();
      params.setState({
        loadingTableisHide: false,
      });
    };

    obj['reset'] = searchReturnData => {
      params.page.page = 1;
      params.page = { ...params.page, ...searchReturnData };
      params.getListForPage();
      params.setState({
        loadingTableisHide: false,
      });
    };

    obj['expand'] = params.state.expand;
    obj['searchNum'] = params.state.searchFieldList.length;
    if (!params.state.expand) {
      obj['searchFieldList'] = params.state.searchFieldList.slice(0, 10); //todo kim-stamp 搜索面板的搜索条件数
    } else {
      params.searchFieldList = params.state.searchFieldList;
    }
    obj['collapse'] = isexpand => {
      if (isexpand) {
        params.setState({
          expand: true,
          searchFieldList: params.state.searchFieldList,
        });
        params.searchFieldList = params.state.searchFieldList || params.searchFieldList;
        // console.log(params.searchFieldList);
      } else {
        params.setState({
          expand: false,
        });
        params.searchFieldList = params.state.searchFieldList;
      }
    };

    return obj;
  },

  //查询api 请求页面
  async getListForPage(params, callback) {
    if (params instanceof Object) {
      var transfer = params;
    } else if (typeof params == 'string') {
      var transfer = { url: params };
    }
    let res = await axios.ajax({
      url: transfer.url,
      params: transfer.data || this.page,
    });
    if (res && callback) {
      callback(res);
    }
    if (res && res.content && res.content.list) {
      let pageListData = res.content.list.map((item, index) => {
        item.key = index;
        return item;
      });
      this.setState({
        pageListData,
        loadingTableisHide: true,
        pagination: QuickTool.pagination(
          res,
          current => {
            this.page.page = current;
            this.getListForPage();
          },
          (current, size) => {
            this.page.page = 1;
            this.page.size = size;
            this.getListForPage();
          },
        ),
      });
    }
  },

  async handleFinishOrder(params) {
    if (params instanceof Object) {
      var transfer = params;
    } else if (typeof params == 'string') {
      var transfer = { url: params };
    }

    let _this = this;
    let res = await axios.ajax({
      url: transfer.url,
      data: {
        params: {
          orderId: _this.state.orderId,
        },
      },
    });
    if (res.code == 0) {
      await this.setState({
        defaultModalVisble: false,
        selectedRowKeys: '',
      });
      this.getListForPage();
      message.success('订单结束成功', 1);
      // message.success('订单结束成功', 1, function () { console.log("ApiTubro --- message关闭回调") })
    }
  },

  async handleMultipleDelect(params) {
    if (params instanceof Object) {
      var transfer = params || {};
    } else if (typeof params == 'string') {
      var transfer = { url: params };
    } else {
      var transfer = {};
    }
    this.setState({ loading: true });

    Modal.info({
      title: '确定要删除',
      content: this.state.selectedRows.map(item => item['user_name']).join(','),
      onOk: async () => {
        let res = axios.ajax({
          url: transfer.url,
          data: { params: transfer.data },
        });
        if (res) {
          await this.getListForPage();
          await this.setState({
            loading: false,
            selectedRows: '',
            selectedRowKeys: '',
          });
          message.success('批量删除成功', 1);
        } else {
          message.success('没有请求url', 1);
        }
      },
    });
  },

  async handleLineDelete(params) {
    if (params instanceof Object) {
      var transfer = params;
    } else if (typeof params == 'string') {
      var transfer = { url: params };
    }

    // if (!params) return Modal.info({title: '信息', content: '请选择一条订单进行结束'});
    let res = await axios.ajax({
      url: transfer.url,
      data: { params: transfer.data },
    });
    if (res && res.code == 0) {
      this.setState({
        orderInfo: res.content,
        defaultModalVisble: true,
      });
    }
  },

  async openOrderMapPage(params) {
    if (params instanceof Object) {
      var transfer = params;
    } else if (typeof params == 'string') {
      var transfer = { url: params };
    }
    // if (!params) {Modal.info({title: '信息', content: '请先选择一条订单'})}
    return window.open(transfer.url, '_blank');
  },
};
